/* $Header: als.h,v 2.4 05/03/02 11:34:22 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  als.h                                           */
/* ==================                                                  */
/*                                                                     */
/* Author:  Tom Przybylski                                             */
/*                                                                     */
/* COMMENT:                                                            */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef ALS_H
#define ALS_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/

#define POWER_OFF_WAIT 512*6  /* 6 seconds off before trying power again    */
#define POWER_ON_WAIT  25     /* approx 50 msec before declaring power "up" */
#define OVERCURRENT    10
#define NO_OVERCURRENT  1
#define ALS_CURRENT_LIMIT 2500 /* about 1/2 sec for full short, 1 sec 50%   */

#define PING_WAIT      250    /*  approx 1/2 sec between pings           */
#define PING_TIMEOUT  2560    /*  approx 5 sec for timeout               */

#define STATUS_WAIT    500    /*  approx 1 sec between status checks */

#define LONG_WAIT     1500    /*  all timeouts approx 3 sec since*/
#define MEDIUM_WAIT   1500    /*  sampler may go away for ~2 sec */


#define ALS_PRESENT        0x01
#define ALS_INIT           0x01
#define ALS_ON_PRESENT     0x03   /* present and enabled */
#define ALS_PARAM_MODIFIED 0x10   /* Parameter modified bit */

#define INJ_IDLE         0           /* STATUS cmd states for injection */
#define INJ_START_CYCLE  1
#define INJ_SYNC         2
#define INJ_CONTINUE     3
#define INJ_BOTTLE_RTN   4
#define INJ_STOPPING     5

#define ALS_MIN_TIME     457747200   /* 4 July 1994 */
#define ALS_MAX_TIME     2035670400  /* 4 July 2044 */

#define AL_IDLE         0
#define AL_TUR_SIZE     1
#define AL_SYR_STOP     2
#define AL_TUR_TOP      3
#define AL_PLUNGER      4
#define AL_HALF         5
#define AL_BUTTON_WAIT  6
#define AL_TUR_CAL      7


/*****************************/
/*   MACROS                  */
/*****************************/


/*****************************/
/*   TYPEDEFS                */
/*****************************/

typedef enum { NO_INJ        = 0,
               INJF          = 1,
               INJB          = 2,
               CONFIG_INJF   = 3,
               CONFIG_INJB   = 4,
               TRAY          = 5,
               CONFIG_TRAY   = 6
              } ALS_MODE;


typedef enum { POWER_OFF      = 0,
               WAITING_PWR_ON = 1,
               POWER_ON       = 2,
               KEEP_OFF       = 3
             } ALS_PWR_MODE;


typedef enum { NO_TOWER        = 0,
               UNMOUNTED_TOWER = 1,
               MOUNTED_TOWER   = 2
             } ALS_TOWER_MODE;


typedef enum
{
   FDEST   = 0,
   BDEST   = 1,
   TDEST   = 3,
   BCRDEST = 4,
   NULLDEST = 5

} BTL_DEST;


typedef struct
{
    U_INT16 comm_port;
    union
    {
       BIT8      char_dsap[DSAP_LEN];    /* destination service access point */
       INT16     int_dsap;
    } dsap;
    union
    {
       BIT8      char_ssap[SSAP_LEN];    /* source service access point */
       INT16     int_ssap;
    } ssap;
    union
    {
       BIT8      char_op[MAX_OPCODE_LEN+1];     /* instruction opcode */
       INT16     int_op;
    } opcode;
    BIT8 want_reply;
} ALS_REPLY;


typedef struct
{
    U_INT16 comm_port;
    union
    {
       BIT8      char_dsap[DSAP_LEN];    /* destination service access point */
       INT16     int_dsap;
    } dsap;
    union
    {
       BIT8      char_ssap[SSAP_LEN];    /* source service access point */
       INT16     int_ssap;
    } ssap;
    union
    {
       BIT8      char_ssap[SSAP_LEN];    /* pass-thru source service access point */
       INT16     int_ssap;
    } ssap2;
} CP_REPLY;



/*****************************/
/*   EXTERNALS               */
/*****************************/

extern U_INT32      Als_Power_Timer;
extern ALS_PWR_MODE Als_Power_Mode;
extern INT32        Als_Power_Filter;
extern INT32        Als_Power_Max;

extern INT32     Ping_Timer;
extern INT32     Q1_Timer;
extern BIT8      Tray_Ping_Sent;
extern BIT8      Tray_Ping[2];
extern BIT8      Q1_Timer_Enable;
extern U_INT8    ALS_Start;

extern ALS_MODE  ALS_Mode;

extern ALS_REPLY Inj_Reply;
extern BIT8      Inj_Busy;

extern BIT8      Tray_Send_Time;
extern BIT8      Tray_Send_Stop;

extern BIT8      Als_Init_Sync;

extern BTL_DEST  Bcr_From_Dest;
extern BTL_DEST  Bcr_To_Dest;
extern INT32     Bcr_From;
extern INT32     Bcr_To;

extern ALS_REPLY Move_Reply;
extern ALS_REPLY Bcr_Reply;
extern CP_REPLY  Cpi1_Reply;
extern CP_REPLY  Cpi1a_Reply;
extern CP_REPLY  Cpi2_Reply;
extern CP_REPLY  Cpi2a_Reply;
extern CP_REPLY  Cptr_Reply;
extern CP_REPLY  Cptra_Reply;

/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

void MonitorSamplerPower (void);
void SendPing (U_INT16 port);
void CheckForAlsReplies(void);
void InitSamplerPFRam (void);

MIO_HDR_TYPE *StartInjMsg (BIT8 **p_out_buf, BIT8 *msg, U_INT16 *len);
MIO_HDR_TYPE *StartTrayMoveMsg (BIT8 **p_out_buf, BIT8 *msg, U_INT16 *len);
MIO_HDR_TYPE *StartBcrMsg (BIT8 **p_out_buf, BIT8 *msg, U_INT16 *len);

void ConfigureSampler (void);
MIO_HDR_TYPE *StartAlsQ1Msg (BIT8 **p_out_buf, BIT8 *msg, U_INT16 *len);
UI_ERR InjectSample (INT32 front, INT32 back, BIT8 no_wait, CMD_TYPE *p_cmd, BIT8 sinject, BIT8 overlap);
UI_ERR MoveSample (BTL_DEST fromdest, INT32 from, BTL_DEST todest, INT32 to, CMD_TYPE *p_cmd);
UI_ERR ReadBarcode (BTL_DEST fromdest, INT32 from, BTL_DEST todest, INT32 to, CMD_TYPE *p_cmd);
UI_ERR ReturnAll (CMD_TYPE *p_cmd);
UI_ERR TrayPos   (CMD_TYPE *p_cmd);
UI_ERR Mix       (CMD_TYPE *p_cmd);

ALS_TOWER_MODE GetAlsTowerMode (POSITION p);
INT32 TranslateInjErr (POSITION p, U_INT16 error);

void SendAlsTime (void);
void SendAlsStop (void);

void AlsTask (void);
void Injector1Task (void);
void Injector2Task (void);
void TrayTask (void);
void CheckForALS(void);

U_INT32 GetAlignMessage(U_INT8 align_state);
U_INT32 GetAlignErrorMessage (U_INT16 err, U_INT8 state);
void    DisplayAlignmentState (POSITION p);

#endif
